package com.test.controller;

import com.alibaba.fastjson.JSONObject;
import com.test.Iservice.IExampleService;
import com.test.pojo.example;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @Description controller控制器
 * @Author Mr.Li
 * @Date 2020/3/10 12:02
 */
@Controller
public class ExampleController {
    @Autowired
  private IExampleService IExampleService;

/**
*@Description: 查询全部
*@Params
*@Return
*@Author Mr.Li
*@Date 2020/3/10 16:28
*/
    @GetMapping("/getAll")
    @ResponseBody
    public List<example> getAll(){
        return IExampleService.selectAll();
    }

/**
*@Description: 删除
*@Params
*@Return
*@Author Mr.Li
*@Date 2020/3/10 16:28
*/
    @DeleteMapping("/delete/{id}")
    @ResponseBody
    public Boolean deleteById(@PathVariable("id") Integer id){
        return IExampleService.deleteExampleById(id);
    }

/**
*@Description:  修改
*@Params
*@Return
*@Author Mr.Li
*@Date 2020/3/10 16:27
*/
    @PutMapping("update")
    @ResponseBody
    public Boolean updateExampleById(@RequestParam("id")Integer id, @RequestParam("sname")String sname, @RequestParam("age")Integer age){
        example example = new example(id, sname, age);
        return IExampleService.updateExampleById(example);
    }

/**
*@Description: 新增
*@Params
*@Return
*@Author Mr.Li
*@Date 2020/3/10 16:27
*/
    @PostMapping("insert")
    @ResponseBody
    public Boolean addExample(@RequestBody example example){
        return IExampleService.addExample(example);
    }

    /**
    *@Description: 获取今天天气 返回hello.jsp
    *@Params
    *@Return
    *@Author Mr.Li
    *@Date 2020/3/10 16:28
    */
    @GetMapping("getWeather/{city}")
    public ModelAndView getWeather(@PathVariable("city")String cityId){
        ModelAndView mv = new ModelAndView("hello");
        JSONObject weatherByCityId  =  IExampleService.getWeatherByCityId(cityId);
        mv.addObject("jsonMessage", weatherByCityId);
        return mv;
    }
}
